<html>
<head>
<meta charset="UTF-8">
<METAHTTP-EQUIV="Pragma"CONTENT="no-cache">
<METAHTTP-EQUIV="Cache-Control"CONTENT="no-cache">
<METAHTTP-EQUIV="Expires"CONTENT="0">
<link rel="stylesheet" href="css/bootstrap.css"> 
<script src="js/jquery.js"></script>
<script src="js/angular.js"></script>
<script type="text/javascript" src="js/bootstrap.js"></script>
</head>
<body ng-app="myApp" ng-controller="siteCtrl">
	<div style="text-align: right;"><iframe src="header.html" style="position: absolute;top: 15px;right: 10px;border: none;width:150px;height:20px"></iframe></div>
	<div class="panel panel-default">
	    <div  class="panel-body"> 
			<table class="table table-bordered table-hover" style="table-layout: fixed;">
			  <caption>公众号管理</caption>
			  <thead>
			  	<colgroup>
				    <col></col>
				    <col></col>
				    <col></col>
				    <col></col>
				  </colgroup>
			    <tr>
			      <th>公众号名称</th>
			      <th>APPID</th>
			      <th>APPSECRET</th>
			      <th>操作</th>
			    </tr>
			  </thead>
			  <tbody>
			     <tr class="{{outerIndex%2==0?'success':'danger'}}" ng-repeat="x in datas" ng-init="outerIndex = $index">
			    	
				    <td>{{ x.name}}</td>
				    <td >{{ x.APPID }}</td>
				    <td >{{ x.APPSECRET }}</td>
				    <td><a href="javascript:void(0)" ng-click="income(x.APPID)">进入公众号</a></td>
				  </tr>
			  </tbody>
			</table>
		</div>
	</div>
	
	<div class="panel panel-default">
		<div class="panel-heading">
	        <h3 class="panel-title">
	        	新增公众号
	        </h3>
	    </div>
	    
	    <div  class="panel-body"> 
	    	<h3 style="color:red">注意：APPID和APPSECRET这两个参数去微信公众平台里面找到复制过来，不要乱填！不要乱填！不要乱填！</h3>
	    	<div class="form-group">
			    <label for="name">公众号名称</label>
			    <input type="text" class="form-control" id="name" ng-model="name" placeholder="请输入公众号名称">
			  </div>
			  <div class="form-group">
			    <label for="APPID">APPID</label>
			    <input type="text" class="form-control" id="APPID"  ng-model="APPID" placeholder="请输入APPID">
			  </div>
			  <div class="form-group">
			    <label for="APPSECRET">APPSECRET</label>
			    <input type="text" class="form-control" id="APPSECRET"  ng-model="APPSECRET" placeholder="请输入APPSECRET">
			  </div>
			  <div style="text-align: right;">
				  <input type="button" class="btn btn-primary" ng-click="submit()"
							value="提交">
			</div>
		</div>
	</div>
	
	<script type="text/javascript">
	
	Date.prototype.format = function(format)
	{
	 var o = {
	 "M+" : this.getMonth()+1, //month
	 "d+" : this.getDate(),    //day
	 "h+" : this.getHours(),   //hour
	 "m+" : this.getMinutes(), //minute
	 "s+" : this.getSeconds(), //second
	 "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
	 "S" : this.getMilliseconds() //millisecond
	 }
	 if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
	 (this.getFullYear()+"").substr(4 - RegExp.$1.length));
	 for(var k in o)if(new RegExp("("+ k +")").test(format))
	 format = format.replace(RegExp.$1,
	 RegExp.$1.length==1 ? o[k] :
	 ("00"+ o[k]).substr((""+ o[k]).length));
	 return format;
	}
	
	var app = angular.module('myApp', []);
    
	app.controller('siteCtrl', function($scope, $http) {
	    
		$http.get('/getAppList').then(function successCallback(response) {
  			console.log(response)
  			$scope.datas=response.data;
        }, function errorCallback(response) {
            // 请求失败执行代码
            console.log(response)
    	});
		
		$scope.submit=function(){
			var data={
					name:$scope.name,
					APPID:$scope.APPID,
					APPSECRET:$scope.APPSECRET
			};
			
			
			console.log(data)
			$http({url:'/insertApp',method:'post',data:JSON.stringify(data),headers:{'Content-Type': 'application/json'}}).then(function successCallback(response) {
				console.log(response)
				if(response.data.code==200){
					$http.get('/getAppList').then(function successCallback(response) {
			  			console.log(response)
			  			$scope.datas=response.data;
			        }, function errorCallback(response) {
			            // 请求失败执行代码
			            console.log(response)
			    	});
				}else{
					alert(response.data.msg);
				}
		    }, function errorCallback(response) {
		            // 请求失败执行代码
		    	console.log(response)
		    });
		}
		$scope.income=function(appid){
			$http.get('/setCurrentAppId?appid='+appid).then(function successCallback(response) {
	  			location.href="datalist.html";
	        }, function errorCallback(response) {
	            // 请求失败执行代码
	            console.log(response)
	    	});
			return false;
		}
		
		$scope.updateApp=function(appid,name){
			$scope.APPID=appid;
			$scope.name=name;
		}
	});
	</script>
</body>
</html>